package com.yanqu.road.dao.impl.wingroom;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.wingroom.message.GuestMessage;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author ：zfr
 * @date ：2022/3/19 17:53
 */
public class WingRoomGuestMessageDaoImpl extends TempDataObjectDao<GuestMessage> {
    public static String insertSql = "INSERT INTO t_u_wingroom_message_detail(userId,messageId,noteStyle,isTop,content," +
            "guestServerId,guestUserId,messageTime,isDelete,isRead) values(?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(GuestMessage data) {
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getMessageId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getNoteStyle()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isTop()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getContent()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getGuestServerId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getGuestUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getMessageTime()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isDelete()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isRead()));

        if (getDb().executeNoneQuery(insertSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateSql = "UPDATE t_u_wingroom_message_detail set isTop=?,isDelete=?,isRead=? where userId=? and messageId=?";
    @Override
    protected boolean updateData(GuestMessage data) {
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isTop()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isDelete()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isRead()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getMessageId()));
        if (getDb().executeNoneQuery(updateSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected GuestMessage parseData(ResultSet rs) throws SQLException {
        GuestMessage data = new GuestMessage();
        data.setUserId(rs.getLong("userId"));
        data.setMessageId(rs.getLong("messageId"));
        data.setNoteStyle(rs.getInt("noteStyle"));
        data.setTop(rs.getBoolean("isTop"));
        data.setContent(rs.getString("content"));
        data.setGuestServerId(rs.getLong("guestServerId"));
        data.setGuestUserId(rs.getLong("guestUserId"));
        data.setMessageTime(rs.getLong("messageTime"));
        data.setDelete(rs.getBoolean("isDelete"));
        data.setRead(rs.getBoolean("isRead"));
        return data;
    }

    public Map<Long, Map<Long, GuestMessage>> getGuestMessageMap(List<Long> userIdList, int messageStoreNum) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Long, Map<Long, GuestMessage>> dataMap = new ConcurrentHashMap<>();
        String sql = "select * from t_u_wingroom_message_detail where isDelete = 0 and userId in ("+ StringUtils.listToString(userIdList, ",") +") order by messageId desc";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            rs = call.executeQuery();
            while (rs.next()) {
                GuestMessage data = parse(rs);
                if (!dataMap.containsKey(data.getUserId())) {
                    dataMap.put(data.getUserId(), new ConcurrentHashMap<>());
                }
                if (data.isTop() || dataMap.get(data.getUserId()).size() < messageStoreNum) {
                    dataMap.get(data.getUserId()).put(data.getMessageId(), data);
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }
}
